jQ(function(){
	jQ("div.my_clips_menu a.clipmanage").addClass("clipmanageonit");
	jQ("#left_clip_list").empty();
	jQ("input.hminput").val(0);
	//analyzeUrl();
	jQ("#mainpart_list_content").data("currentpage",0);
	jQ("#clipmanagehead li").click(function(){
		var qb=jQ(this).attr("queryby");
		if(qb=="cache"||qb=="recycle"){
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_"+qb+".html";
		}else{
			window.location.href=jQ("#ctxVal").val()+"/myclip/clip_list.html#"+qb;
		}
	});
	var usertype=eval(jQ("#var_usertype").val());
	if(usertype==2){
		jQ("#clipmanagehead li[queryby='share']").hide();
		jQ("#invitecount").hide();
		jQ("#clipmanagehead li.nonequery").css("width","432px");
	}
	loadRecycleMainpartRecords();
	jQ("#viewopt").dialog({
		modal: true,
		bgiframe: true,
		autoOpen: false,
		closeOnEscape:false,
		draggable:false,
		height:200,
		buttons: {
			
			"确定": function() {
				var opt=jQ("#viewopt");
				var triggerfun=opt.data("function");
				opt.trigger(triggerfun);
				opt.removeData("function");
				jQ("#viewopt_p").hide();
				jQ("#viewopt_img").show();
				activeBtnOnDialog("viewopt",false);
				//jQ(this).dialog('close');
			},
			"取消": function() {
				var viewopt=jQ("#viewopt");
				var triggerfun=jQ("#viewopt").data("function");
				viewopt.unbind(triggerfun);
				viewopt.find("input.eventid").val("");
				viewopt.removeData("function");
				jQ(this).dialog('close');
			}
		},
		close: function() {
			activeBtnOnDialog("viewopt",true);
			jQ("#viewopt_p").show();
			jQ("#viewopt_img").hide();
		},
		open:function(){
		}
	});
});

function analyzeUrl(){
	var currentpage=eval(getParameter("currentpage"));
	if(isNaN(currentpage)||currentpage==null){
		jQ("#mainpart_list_content").data("currentpage",0);
	}else{
		jQ("#mainpart_list_content").data("currentpage",currentpage);
	}
}

var PAGESIZE=15;
function loadRecycleMainpartRecords(){
	var currentpage=eval(jQ("#mainpart_list_content").data("currentpage"));
	var queryby="recycle";
	jQ.ajax({
		type:"POST",
		url: "manageClipAjax!loadMainpartRecords.action",
		data:{
			queryby:queryby,
			currentpage:currentpage
		},
		success:function(data){
			var alerttotalcount=eval(jQ(data).find("alerttotalcount").text());
			var memototalcount=eval(jQ(data).find("memototalcount").text());
			var invitetotalcount=eval(jQ(data).find("invitetotalcount").text());
			var cachetotalcount=eval(jQ(data).find("cachetotalcount").text());
			var recycletotalcount=eval(jQ(data).find("recycletotalcount").text());
			
			jQ("#memocount").data("count",memototalcount);
			jQ("#invitecount").data("count",invitetotalcount);
			jQ("#alertcount").data("count",alerttotalcount);
			jQ("#cachecount").data("count",cachetotalcount);
			jQ("#recyclecount").data("count",recycletotalcount);
			
			alerttotalcount=alerttotalcount>99?"99+":alerttotalcount;
			memototalcount=memototalcount>99?"99+":memototalcount;
			invitetotalcount=invitetotalcount>99?"99+":invitetotalcount;
			cachetotalcount=cachetotalcount>99?"99+":cachetotalcount;
			recycletotalcount=recycletotalcount>99?"99+":recycletotalcount;
			
			jQ("#memocount>span.count").empty().append(memototalcount);
			jQ("#invitecount>span.count").empty().append(invitetotalcount);
			jQ("#alertcount>span.count").empty().append(alerttotalcount);
			jQ("#cachecount>span.count").empty().append(cachetotalcount);
			jQ("#recyclecount>span.count").empty().append(recycletotalcount);
			
			var currentpage=eval(jQ(data).find("currentpage").text());
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			var totalcount=eval(jQ(data).find("totalcount").text());
			var pagecount=eval(jQ(data).find("pagecount").text());
			jQ("#mainpart_list_content").data("pagecount",pagecount);
			var currentrecordcount=eval(jQ(data).find("currentrecordcount").text());
			if(totalcount>1)
				jQ("b.clippage_currentrecordcount").empty().append(currentpage*PAGESIZE+1+"-"+(currentpage*PAGESIZE+currentrecordcount));
			else
				jQ("b.clippage_currentrecordcount").empty().append(totalcount);
			jQ("b.clippage_totalcount").empty().append(totalcount);
			if(jQ(data).find("ClipEvent").length==0){
				jQ("#mainpart_list_content").empty().append("<tr><td colspan=6 style='font-size: 12px; text-align: center;'>暂无内容</td></tr>");
				return;
			}
			var cliphtml="";
			jQ(data).find("ClipEvent").each(function(){
					var id=eval(jQ(this).attr("id"));//
					var uid=jQ(this).find("unameid").text();//
					var userlogo=jQ(this).find("unamelogo").text();
					var title=jQ(this).find("title").text();//
					//var eventtype=jQ(this).find("eventtype").text();//
					var isshareaccept=eval(jQ(this).find("isshareaccept").text());//
					var isinviteaccept=eval(jQ(this).find("isinviteaccept").text());//
					var uname=jQ(this).find("uname").text();
					var reliedclip=eval(jQ(this).find("reliedclip").text());
					var timesituation=eval(jQ(this).find("timesituation").text());
					var cliptype=eval(jQ(this).find("cliptype").text());
					var starttime=eval(jQ(this).find("starttime").text());
					var endtime=eval(jQ(this).find("endtime").text());
					var isuserself=eval(jQ(this).attr("isuserself"));
					var processstatus=eval(jQ(this).find("processstatus").text());
					var iswholeday=eval(jQ(this).find("iswholeday").text());
					var deadlinetime=eval(jQ(this).find("deadlinetime").text());
					var createtime=eval(jQ(this).find("createtime").text());
					var isexpire=eval(jQ(this).find("isexpire").text());
					var clipstatus=eval(jQ(this).find("clipstatus").text());
					cliphtml+="<tr id='"+queryby+"-"+id+"'>";
					cliphtml+="<td class=\"check\">";
					cliphtml+="<div class=\"clip_checkbox\">";
					cliphtml+="</div>";
					cliphtml+="</td>";
					cliphtml+="<td class=\"content\">";
					cliphtml+="<div class=\"other\">";
					cliphtml+="<span class=\"head\" style=\"background:url('"+userlogo+"');\"></span>";
					cliphtml+="<span class=\"name\">"+uname+"</span>";
					cliphtml+="<span class=\"title\">"+title+"</span>";
					cliphtml+="<span class=\"time\">";
					if(processstatus==0){
						cliphtml+="<b class='clip_ing' title='进行中'></b>";
					}else if(processstatus==1){
						cliphtml+="<b class=\"clip_over\" title='已结束'></b>";
					}else if(processstatus==2){
						cliphtml+="<b class=\"clip_stop\" title='已停止'></b>";
					}
					cliphtml+="<span class=\"timevalue\">";
					if(iswholeday){
						cliphtml+="<b >开始于</b>";
						cliphtml+="<b class=\"first\">"+formateDetailTime(starttime)+"</b>";
					}else{
						cliphtml+="<b class=\"first\">"+formateDetailTime(starttime)+"</b>";
						cliphtml+="<b class=\"second\">"+formateDetailTime(endtime)+"</b>";
					}
					cliphtml+="</span>";
					cliphtml+="</span>";
					cliphtml+="<span class=\"linetime\">";
					if(deadlinetime<=0){
						cliphtml+="<span>暂无</span>";
					}else{
						cliphtml+="<b >回复截止：";
						if(isexpire)
							cliphtml+="<span class='expire'>已过期</span>";
						cliphtml+="</b>";
						cliphtml+="	<b >"+formateDetailTime(deadlinetime)+"</b>";
					}
					cliphtml+="</span>";
					cliphtml+="<span class=\"createtime\">";
					cliphtml+=formatCreatetime(createtime);
					cliphtml+="</span>";
					cliphtml+="</div>";
					cliphtml+="</td>";
					cliphtml+="</tr>";
			});
			jQ("#mainpart_list_content").empty().append(cliphtml);
			jQ("#mainpart_list_content tr td.check").unbind("click").bind("click",checkLine);
			jQ("#mainpart_list_content tr td.content").unbind("click").bind("click",detailRecycle);
			initPaging(currentpage,pagecount);
		}
	});
}

function initPaging(currentpage,pagec){
	jQ("#pagingcontent").empty();
	jQ("#pagingcount").empty();
	var pagecount=pagec;
	if(pagecount==1){
		jQ("#pagingcontent").hide();
		jQ("#pagecount").hide();
		return;
	}else{
		var pagemin=currentpage-2;
		var pagemax=currentpage+2;
		if(pagemin<0){
			pagemin=0;
		}
		if(pagemax>pagecount-1){
			pagemax=pagecount-1;
		}
		var html="";
		html+="<a page='first'><span >&lt;&lt;</span></a>";
		for(var i=pagemin;i<=pagemax;i++){
			if(i==currentpage){
				html+="<b ><span>"+(i+1)+"</span></b>";
			}else
				html+="<a page='"+i+"'><span>"+(i+1)+"</span></a>";
		}
		html+="<a page='last'><span >&gt;&gt;</span></a>";
		jQ("#pagingcontent").append(html);
		jQ("#pagingcontent a").unbind("click").bind("click",function(){
			var page=jQ(this).attr("page");
			var currentpage=0;
			if(page=="first"){
				currentpage=0;
			}else if(page=="last"){
				currentpage=pagecount-1;
			}else{
				currentpage=eval(page);
			}
			jQ("#mainpart_list_content").data("currentpage",currentpage);
			loadMainpartRecords();
			//loadAllMemo(currentpage);
		});
		jQ("#pagingcontent").show();
		jQ("#pagingcount").append("共有"+pagecount+"页").show();
	}
}

function sel_allclick(obj){
	var trlength=jQ("#mainpart_list_content tr").length;
	var ison=jQ(obj).hasClass("sel_allon");
	if(trlength==0){
		if(ison){
			jQ("a.sel_all").removeClass("sel_allon");
		}else{
			jQ("a.sel_all").addClass("sel_allon");
		}
	}else{
		if(ison){
			jQ("a.sel_all").removeClass("sel_allon");
			jQ("#mainpart_list_content tr td.check").each(function(){
				if(jQ(this).find("div").hasClass("check")){
					jQ(this).find("div").removeClass("check");
					var trobj=jQ(this).parent();
					trobj.data("check",false);
				}
			});
		}else{
			jQ("a.sel_all").addClass("sel_allon");
			jQ("#mainpart_list_content tr td.check").each(function(){
				if(!jQ(this).find("div").hasClass("check")){
					jQ(this).find("div").addClass("check");
					var trobj=jQ(this).parent();
					trobj.data("check",true);
				}
			});
		}
	}
}

function detailRecycle(){
	var trobj=jQ(this).parent();
	var ids=trobj.attr("id");
	var eventid=ids.split("-")[1];
	window.location.href=jQ("#ctxVal").val()+"/myclip/recycle_detail.html?eventid="+eventid;
}

function checkLine(){
	var trobj=jQ(this).parent();
	if(jQ(this).find("div").hasClass("check")){
		jQ(this).find("div").removeClass("check");
		trobj.data("check",false);
	}else{
		jQ(this).find("div").addClass("check");
		trobj.data("check",true);
	}
}

function selected2EjectEvents(){
	var info="";
	var title="";
	info="您确定彻底删除这些档期？";
	title="删除档期";
	var opt=jQ("#viewopt");
	opt.bind("ejectClipEvents",ejectClipEvents);
	opt.data("function","ejectClipEvents")
	jQ("#viewopt_p").empty().append(info);
	jQ("#viewopt").dialog( "option", "title", title);
	jQ("#viewopt").dialog("open");
}

function ejectClipEvents(){
	jQ("#mainpart_list_content td.check div.check").each(function(){
		var trobj=jQ(this).parent().parent();
		var trid=trobj.attr("id");
		var eventid=eval(trid.split("-")[1]);
		jQ.ajax({
			type: "POST",
			url:"eventAjax!deleteEvent.action",
			data:{
				eventid:eventid
			},
			success:function(data){
				if(data!=null){
					var dataObj=eval("("+data+")");//转换为json对象
					if(dataObj['result']){
						jQ("#clipevent-"+eventid).remove();
					}else{
						
					}
				}
			}
		});
	});
	jQ("#viewopt").dialog("close");
	loadRecycleMainpartRecords();
}

function formateDetailTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy-MM-dd HH:mm");
}

function formateTitleTime(daytime){
	var temp=new Date();
	temp.setTime(eval(daytime));
	return temp.pattern("yyyy年MM月dd日");
}

